package com.abc.cloud.service.impl;

import com.abc.cloud.common.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Query;

import com.abc.cloud.dao.FeedbackDao;
import com.abc.cloud.entity.FeedbackEntity;
import com.abc.cloud.service.FeedbackService;

import javax.xml.crypto.Data;



@Slf4j
@Service("feedbackService")
public class FeedbackServiceImpl extends ServiceImpl<FeedbackDao, FeedbackEntity> implements FeedbackService {

    @Autowired
    private FeedbackService feedbackService;

    @Override
    public void save(FeedbackEntity feedback,Long num) {
        Date date = new Date();
        feedback.setCreateTime(date);
        feedback.setUserId(UserHolder.getUser().getId());
        feedback.setUpdateTime(date);
        feedback.setStatus(1);
        feedbackService.save(feedback);
    }

    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        IPage<FeedbackEntity> page = this.page(
                new Query<FeedbackEntity>().getPage(params),
                new QueryWrapper<FeedbackEntity>()
        );

        return new PageUtil(page);
    }

    @Override
    public PageUtil getAllList(Map<String, Object> params) {
        Integer pageSize = 10;
        Integer currPage = 1;
        String searchContent = "";
        if(params.get("pageSize") != null){
            pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
        }
        if(params.get("currPage") != null){
            currPage = Integer.parseInt(String.valueOf(params.get("currPage")));
        }
        if(params.get("searchContent") != null){
            searchContent = String.valueOf(params.get("searchContent"));
        }
        pageSize = pageSize > 0 ? pageSize : 1;
        Integer totalCount = baseMapper.getAllCount(searchContent);
        Integer totalPage = totalCount / pageSize + (totalCount%pageSize > 0 ? 1 : 0);       //总页数
        List<FeedbackEntity> feedbackEntityList = baseMapper.getAllList((currPage-1)*pageSize ,pageSize,searchContent);
        log.info("pageSize = {},currPage = {},searchContent = {}",pageSize,currPage,searchContent);
        log.info("list = ", feedbackEntityList);
        return new PageUtil(feedbackEntityList,pageSize,currPage,totalCount,totalPage);
    }

    @Override
    public void updateFeedIdsStatus(List<Long> feedIds, Integer status) {
        baseMapper.updateFeedStatus(feedIds,status);
    }
}